package com.edwin.websitebe.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edwin.websitebe.model.Product;
import com.edwin.websitebe.model.Sample;
import com.edwin.websitebe.service.ProductService;
import com.edwin.websitebe.util.OSSUtil;
import com.edwin.websitebe.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService service;

    @GetMapping("/page")
    public ResultVO page(@RequestParam Long page, @RequestParam Long pageSize, @RequestParam(required = false) String productName, @RequestParam(required = false) Long productShowTypeId) {

        LambdaQueryWrapper<Product> wrapper = new QueryWrapper<Product>().lambda()
                .like(Objects.nonNull(productName), Product::getProductName, productName)
                .eq(Objects.nonNull(productShowTypeId), Product::getProductShowTypeId, productShowTypeId)
                .orderByAsc(Product::getSort)
                .orderByDesc(Product::getCreateTime);
        Page<Product> p = service.page(Page.of(page, pageSize), wrapper);


        return new ResultVO().success(p);
    }

    @GetMapping("/getById/{id}")
    public ResultVO getById(@PathVariable("id") Long id) {
        Product sample = service.getById(id);
        return new ResultVO().success(sample);
    }

    @PostMapping("")
    public ResultVO saveOrUpdate(@RequestBody Product product) {
        boolean result = service.saveOrUpdate(product);
        return result ? new ResultVO().success() : new ResultVO().fail();
    }

    @DeleteMapping("/delete")
    public ResultVO delete(@RequestParam Long id) {
        boolean result = service.removeById(id);
        return new ResultVO().success();
    }

    @PostMapping("/uploadHomeImg")
    public ResultVO uploadHomeImg(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品首页图片: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-home" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品主页图片上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setHomeImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadSeriesImg")
    public ResultVO uploadSeriesImg(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品中心图片: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-series" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品中心图片上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setSeriesImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadBigSeriesImg")
    public ResultVO uploadBigSeriesImg(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品中心大图: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-bigSeries" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品中心大图上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setBigSeriesImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadDetailTopImg")
    public ResultVO uploadDetailTopImg(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品详情大图: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-detailTop" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品中心大图上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setDetailTopImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadVideoImg")
    public ResultVO uploadVideoImg(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品视频封面: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-videoImg" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品视频封面上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setVideoImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadPairImg")
    public ResultVO uploadPairImgUrl(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传爆款搭配图片: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-pairImg" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品爆款搭配上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setPairImgUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadVideo")
    public ResultVO uploadVideo(MultipartFile file, @RequestParam String id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品视频: id: {}", id);
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String objName = "img/product/" + id + "-" + IdUtil.simpleUUID() + "-video" + extend;
        String url = OSSUtil.uploadFile(objName, file);
        log.info("产品视频上传完成: id: {}, url: {}", id, url);
        log.info("开始更新产品数据");
        Product product = service.getById(id);
        product.setVideoUrl(url);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadIntroImg")
    public ResultVO uploadIntroImg(MultipartFile[] fileList, Long id, HttpServletRequest request) throws IOException {
        log.info("开始上传产品介绍图片: id: {}", id);

        List<MultipartFile> files = Arrays.asList(fileList);

        List<String> fullUrlList = new ArrayList<>(files.size());
        for (MultipartFile file : files) {
            String filename = file.getOriginalFilename();
            String extend = filename.substring(filename.lastIndexOf("."));
            String objName = "img/product/" + id + "-" + IdUtil.getSnowflakeNextIdStr() + "-introImg" + extend;
            String url = OSSUtil.uploadFile(objName, file);
            fullUrlList.add(url);
        }
        String introImgUrl = StringUtils.join(fullUrlList, ",");
        Product product = service.getById(id);
        product.setIntroImgUrl(introImgUrl);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadHotImg")
    public ResultVO uploadHotImg(MultipartFile[] fileList, Long id, HttpServletRequest request) throws IOException {
        log.info("开始上传热销游戏图片: id: {}", id);

        List<MultipartFile> files = Arrays.asList(fileList);

        List<String> fullUrlList = new ArrayList<>(files.size());
        for (MultipartFile file : files) {
            String filename = file.getOriginalFilename();
            String extend = filename.substring(filename.lastIndexOf("."));
            String objName = "img/product/" + id + "-" + IdUtil.getSnowflakeNextIdStr() + "-hotImg" + extend;
            String url = OSSUtil.uploadFile(objName, file);
            fullUrlList.add(url);
        }
        String introImgUrl = StringUtils.join(fullUrlList, ",");
        Product product = service.getById(id);
        product.setHotImgUrl(introImgUrl);
        service.updateById(product);
        return new ResultVO().success();
    }

    @PostMapping("/uploadLandingImg")
    public ResultVO uploadLandingImg(MultipartFile[] fileList, Long id, HttpServletRequest request) throws IOException {
        log.info("开始上传落地案例图片: id: {}", id);

        List<MultipartFile> files = Arrays.asList(fileList);

        List<String> fullUrlList = new ArrayList<>(files.size());
        for (MultipartFile file : files) {
            String filename = file.getOriginalFilename();
            String extend = filename.substring(filename.lastIndexOf("."));
            String objName = "img/product/" + id + "-" + IdUtil.getSnowflakeNextIdStr() + "-landing" + extend;
            String url = OSSUtil.uploadFile(objName, file);
            fullUrlList.add(url);
        }
        String introImgUrl = StringUtils.join(fullUrlList, ",");
        Product product = service.getById(id);
        product.setLandingImgUrl(introImgUrl);
        service.updateById(product);
        return new ResultVO().success();
    }

}
